﻿<?php
	include(ROOTPATH .'include'. DS .'protect.php');
	include ROOTPATH .'include'. DS .'isMyQuery.php';
	include ROOTPATH .'class'. DS .'mail.calss.php';
	
	$do				= getQuery('do')  ;
	$userName		= getQuery('userName')	  ;
	$nickName		= getQuery('nickName')	  ;
	$password		= getQuery('password')	  ;
	$password1		= getQuery('password1') ;
	$gender			= getQuery('gender')  ;
	$province		= getQuery('province') ;
	
	
	
	//change
	$userName 		= strtolower(rtrim($userName));
	$nickName		= rtrim($nickName); //strtolower(rtrim($nickName));
	$password		= password($password );
	$password1		= password($password1  );
	
	
	
	
	

switch($do){
	case 'checkEmail' 	: userCheckEmail($userName) ;
	break;
	
	case 'checkNickName' 	: userCheckNickName($nickName) ;
	break;
	
	case 'reg'			: reg($userName,$password,$password1);		
	break;
}

function checkEmail($userName = ''){
	global $dbs;
	if('' == $userName)
		return false;
	$lists = $dbs -> select('select * From `'. DB_PRE .'user` WHERE `userName` = "'. $userName .'" ');
	if( count($lists) == 0 ){
		$flag = true;
	}else{
		$flag = false;
	}
	return $flag;
}

function userCheckEmail($userName){
	if(isEmail($userName)){
		if( checkEmail($userName) ){
			echo '<i status="ok" class="ok">恭喜.此用户名可以注册</i>' ;
			return true;
		}else{
			echo '<i status="false" class="error"> 已被人占用,请换另外一个.</i>' ;
			return false;
		}
	}else{
		//exit( '<i status="false" class="error"> 邮件格式不正确.</i>') ;
		exit(infoPage('失败.邮件格式不正确.',WEB_ROOT_PATH.'?option=reg'));
		return false;
	}
}



//////////////////////////////////////////////////
function checkNickName($nickName = ''){
	global $dbs;
	if('' == $nickName)
		return false;
	$lists = $dbs -> select('select * From `'. DB_PRE .'user` WHERE `nickName` = "'. $nickName .'" ');
	if( count($lists) == 0 ){
		$flag = true;
	}else{
		$flag = false;
	}
	return $flag;
}
function userCheckNickName($nickName){
	//if(isNickName($nickName)){
	if(1){
		if( checkNickName($nickName) ){
			echo '<i status="ok" class="ok">恭喜.此昵称可以使用</i>' ;
			return true;
		}else{
			echo '<i status="false" class="error"> 已被人占用,请换另外昵称.</i>' ;
			return false;
		}
	}else{
		//exit( '<i status="false" class="error"> 邮件格式不正确.</i>') ;
		exit(infoPage('昵称格式不正确,必须是字母、数字、下划线.',WEB_ROOT_PATH.'?option=reg'));
		return false;
	}
}




function reg($userName,$password,$password1){
	global $dbs,$flag,$dataType,$nickName;
	if( $userName == '' || $nickName == '' || $password == '' || $password1 == '' )
		exit(infoPage('失败.请把资料填写完整.',WEB_ROOT_PATH.'?option=reg'));

	if( strlen($password) < PASS_LEN )
		exit(infoPage('失败.密码长度必须大于6位.',WEB_ROOT_PATH.'?option=reg'));
	if( $password !=  $password1  )
		exit(infoPage('失败.两次密码不相同.请检查.',WEB_ROOT_PATH.'?option=reg'));
	if(!isEmail($userName))
		exit(infoPage('失败.这不是一个正确的Email.',WEB_ROOT_PATH.'?option=reg'));

	if( !checkEmail($userName) ){
		exit( '{"status":"error","msg":"<strong>'.$userName.'</strong> 已被人占用,请换另外一个."}') ;
	}
	if( !checkNickName($nickName) ){
		exit( '{"status":"error","msg":"<strong>'.$nickName.'</strong> 已被人占用,请换另外一个."}') ;
	}

	if( checkEmail($userName) ){
		$array['userName'] 		= $userName;
		$array['passWord'] 		= $password;
		$array['nickName'] 		= $nickName;		
		$array['ip'] 			= ip();
		$array['powder'] 		= 1;
		$array['loginTimes'] 	= 1 ;
		$array['time'] 			= date('Y-m-d H:i:s',time());
		$array['randomPass']	= rand(1000000000,99999999999);
		$user_id = $dbs -> insert( DB_PRE .'user' , $array, true );
		$array['id'] = $user_id ;
		
		initUserLogin($array);
		$user = new users();
		
		$mail = new email();
		
		$sengMail = $mail -> send($to='13640661@qq.com',$name='王',$title = '测试邮件',$body  ='测试邮件正文');
		if( $sengMail )
			echo '{"status":"OK","msg":"注册成功. 一封提示邮件已经发送至：<strong>'.$userName.'</strong>. 页面<b class=\"time\">'.REG_SUCCESS_JUMP_TIME.'</b>秒后跳转..."}' ;
		else
			echo '{"status":"OK","msg":"注册成功.您的邮箱没有接受邮件.页面<b class=\"time\">'.REG_SUCCESS_JUMP_TIME.'</b>秒后跳转..."}' ;	
			
	}else{
		echo '{"status":"error","msg":"<strong>'.$userName.'</strong> 已被人占用,请换另外一个."}' ;
	}	
}
?>